﻿using System;
using ElevatorSpeeches.Framework;
using System.Web.UI;
using System.Web;

namespace ElevatorSpeeches.UI.WebForms
{
    /// <summary>
    /// Displays a Speech Topic, based on the "Url" QueryString.
    /// </summary>
    /// <remarks>
    /// If Url does not exist, displays nothing.
    /// 
    /// 
    /// </remarks>
    public partial class DisplaySpeechTopic : Page
    {
        protected SpeechTopic PageSpeechTopic;

        private void _LoadPageTopic()
        {
            if (String.IsNullOrEmpty(Request.QueryString["Url"]))
                return;
            PageSpeechTopic = SpeechTopic.FindByPrettyUrl(Request.QueryString["Url"]);
            this.Page.Title = PageSpeechTopic.Title + Constants.WEBSITE_TITLE_POSTFIX;
            TitleLabel.Text = PageSpeechTopic.Title;
            
            // TODO: why is this so dumb, just because it's in a LoginView?
            if (HttpContext.Current.User.Identity.IsAuthenticated)
                ((ElevatorSpeeches.UI.WebForms.Controls.CreateSpeech) CreateSpeechLoginView.FindControl("createSpeech")).Topic = PageSpeechTopic;

            SpeechRepeater.DataSource = PageSpeechTopic.Speeches;
            SpeechRepeater.DataBind();            
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                _LoadPageTopic();            
        }        
    }
}